package com.tanhua.manage.controller;
import com.tanhua.manage.service.UsersService;
import com.tanhua.manage.vo.PageResult;
import com.tanhua.manage.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("management/manage")
public class UsersController {
    @Autowired
    private UsersService usersService;
   @GetMapping("users")
    public ResponseEntity<PageResult> queryUsersList(@RequestParam(value = "pagesize",defaultValue = "10",required = false)Integer pageSize,
                                                     @RequestParam(value = "page",defaultValue = "1",required = false) Integer page,
                                                     @RequestParam(value = "id",required = false) Long id,
                                                     @RequestParam(value = "nickname",required = false) String nickName,
                                                     @RequestParam(value = "city",required = false)String city){

       try {
           PageResult pageResult =  this.usersService.queryUsersList(page,pageSize,id,nickName,city);
           if (pageResult!=null){
               return ResponseEntity.ok(pageResult);
           }
       } catch (Exception e) {
           e.printStackTrace();
       }

       return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }



    @GetMapping("users/{userID}")
    public ResponseEntity<UserVo> queryUsers(@PathVariable("userID") Long userId){
        try {
            UserVo userVo = this.usersService.queryUsers(userId);
            if (userVo!=null){
                return ResponseEntity.ok(userVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
